//
//  KBStatusCell.swift
//  KimboWeiBoSwift
//
//  Created by kimbo on 15/12/20.
//  Copyright © 2015年 kimbo. All rights reserved.
//

import UIKit

/// 微博cell的基类
class KBStatusCell: UITableViewCell {

    // MARK: - 属性
    /// 配图的宽约束
    var pictureViewWidthCon: NSLayoutConstraint?
    /// 配图的高约束
    var pictureViewHeightCon: NSLayoutConstraint?
    
    var status: KBStatus? { //控制器传入微博数据时,顺手传给topView
        didSet {
            topView.status = status
            contentLabel.text = status?.text
            pictureView.status = status
            /// 获取不同配图的对应CGSize
            let size = pictureView.calcItemSize()
            pictureViewWidthCon?.constant = size.width
            pictureViewHeightCon?.constant = size.height
            
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        // 设置UI
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 设置UI
    func setupUI() {
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
    
        //添加约束
        /* 顶部视图 */
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        
        /* 微博文本 */
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: 8, y: 8))
        
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.width - 2 * 8))
        
//        /* 配图 */
//        let con = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 290, height: 290), offset: CGPoint(x: 0, y: 8))
//        
//        pictureViewWidthCon = pictureView.ff_Constraint(con, attribute: NSLayoutAttribute.Width)
//        
//        pictureViewHeightCon = pictureView.ff_Constraint(con, attribute: NSLayoutAttribute.Height)
        
        /* 底部视图 */
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -8, y: 8))
        
        // TODO:当配图高度随机时，系统会报次约束冲突
//        contentView.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bottomView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    }
    
    /// 返回计算好的cell的高度
    func calcCellHeight(status: KBStatus) -> CGFloat {
        
        // 系统是先调用有多少行cell，在调用cell的高度，然后再调用返回怎样的cell;
        // 所以计算行高时要传入一个模型数据
        self.status = status
        
        layoutIfNeeded()
        
        return CGRectGetMaxY(bottomView.frame)
    }
    
    // MARK: - 懒加载
    /* 顶部视图 */
    private lazy var topView: KBTopView = KBTopView()
    /* 微博文本 */
    lazy var contentLabel: UILabel = {
       
        let contentLabel = UILabel(color: UIColor.darkGrayColor(), fontSize: 14)
        
        contentLabel.numberOfLines = 0
        
        return contentLabel
    }()
    /* 配图 */
    lazy var pictureView: KBPictureView = KBPictureView()
    /* 底部视图 */
    lazy var bottomView: KBBottomView = KBBottomView()
}


